﻿using Kingdee.BOS.Contracts;
using Kingdee.BOS;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System.Collections.Generic;
using System;
using System.ComponentModel;
using System.Text;
using Kingdee.BOS.App.Data;

namespace RCE.K3.YGJD.Business.PlugIn.SAL.Report
{
    [HotUpdate]
    [Description("产品利润分析表")]
    public class ProductProfitAnalysisEdit : SysReportBaseService
    {

        public string multiOrgNameValues;
        private string startDate;
        private string endDate;
        private string startCust;
        private string endCust;
        private string startMaterial;
        private string endMaterial;
        private string FAmountSource;
        private string FSummaryBasis;
        private string FGrossProfitMarginCalc;
        private string GSSYBField;
        private string invJoinField;
        private string SummaryBasisField;
        private string grossProfitMarginCalcField;
        private string jGrossProfitMarginCalcField;
        private string HeaderFieldTax;


        #region 重写

        /// <summary>
        /// 初始化
        /// </summary>
        public override void Initialize()
        {
            //初始化
            base.Initialize();

            // 简单账表类型：普通、树形、分页
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            this.IsCreateTempTableByPlugin = false;

            //是否分组汇总
            this.ReportProperty.IsGroupSummary = true;
        }

        /// <summary>
        /// 获取过滤条件信息(构造单据信息)
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            ReportTitles reportTitles = new ReportTitles();
            reportTitles.AddTitle("FHeaderOrgId", multiOrgNameValues);
            reportTitles.AddTitle("FHeaderDate", startDate + "至" + endDate);
            reportTitles.AddTitle("FHeaderCust", startCust + "至" + endCust);
            reportTitles.AddTitle("FHeaderMaterial", startMaterial + "至" + endMaterial);
            return reportTitles;
        }

        /// <summary>
        /// 设置单据列
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            ReportHeader header = new ReportHeader();
            // 编号
            switch (FSummaryBasis)
            {
                case "1":
                    //SummaryBasisField = "FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName";
                    header.AddChild("FMaterialNumber", new LocaleValue("物料编码", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FMaterialName", new LocaleValue("物料名称", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FMaterialSpecification", new LocaleValue("规格型号", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FUnitName", new LocaleValue("单位", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FBMSYBNumber", new LocaleValue("物料归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FBMSYBName", new LocaleValue("物料归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    break;
                case "2":
                    //SummaryBasisField = "FCustNumber,FCustName";
                    header.AddChild("FCustNumber", new LocaleValue("客户编码", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FCustName", new LocaleValue("客户名称", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FKHSYBNumber", new LocaleValue("客户归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FKHSYBName", new LocaleValue("客户归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    break;
                case "3":
                    //SummaryBasisField = "FCustGroupNumber,FCustGroupName";
                    header.AddChild("FCustGroupNumber", new LocaleValue("客户分组编码", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FCustGroupName", new LocaleValue("客户分组名称", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    break;
                case "4":
                    //SummaryBasisField = "FMaterialGroupNumber,FMaterialGroupName";
                    header.AddChild("FMaterialGroupNumber", new LocaleValue("物料分组编码", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FMaterialGroupName", new LocaleValue("物料分组名称", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    break;
                case "5":
                    //SummaryBasisField = "FKHSYBNumber,FKHSYBName";
                    header.AddChild("FKHSYBNumber", new LocaleValue("客户归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FKHSYBName", new LocaleValue("客户归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    break;
                case "6":
                    //SummaryBasisField = "FBMSYBNumber,FBMSYBName";
                    header.AddChild("FBMSYBNumber", new LocaleValue("物料归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FBMSYBName", new LocaleValue("物料归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    break;
                default:
                    //SummaryBasisField = "FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName";
                    header.AddChild("FMaterialNumber", new LocaleValue("物料编码", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FMaterialName", new LocaleValue("物料名称", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FMaterialSpecification", new LocaleValue("规格型号", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FUnitName", new LocaleValue("单位", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FBMSYBNumber", new LocaleValue("物料归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    header.AddChild("FBMSYBName", new LocaleValue("物料归属事业部", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
                    break;
            }

            header.AddChild("FSaleQty", new LocaleValue("收入&销售数量", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FSalePrice", new LocaleValue("收入&销售单价", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FSaleAmount", new LocaleValue("收入&销售金额", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FSaleTaxAmount", new LocaleValue("收入&销售税额", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FSaleAllAmount", new LocaleValue("收入&销售价税合计", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FPurPrice", new LocaleValue("成本&采购单价", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FPurAmount", new LocaleValue("成本&采购金额", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FPurTaxAmount", new LocaleValue("成本&采购税额", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FPurAllAmount", new LocaleValue("成本&采购价税合计", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FInvQty", new LocaleValue("库存&库存数量", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FInvPrice", new LocaleValue("库存&库存单价", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);

            header.AddChild("FInvAmount", new LocaleValue("库存&库存金额", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FNoTaxProfit", new LocaleValue("销售毛利&未税毛利", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FTaxDiffAmount", new LocaleValue("销售毛利&税额差", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FGrossProfit", new LocaleValue("销售毛利&毛利率", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FPeriodEexpense", new LocaleValue("期间费用", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FSalesNetProfit", new LocaleValue("销售净利&销售净利", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("FSalesNetProfitLv", new LocaleValue("销售净利&销售净利率", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            return header;
        }

        /// <summary>
        /// 创建临时报表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="tableName"></param>
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            base.BuilderReportSqlAndTempTable(filter, tableName);
        }

        /// <summary>
        /// 设置汇总列信息
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override List<SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new SummaryField("FSaleQty", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FSaleAmount", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FSaleTaxAmount", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FSaleAllAmount", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FPurAmount", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FPurTaxAmount", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FPurAllAmount", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FInvQty", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FInvAmount", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FNoTaxProfit", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FTaxDiffAmount", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FPeriodEexpense", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("FSalesNetProfit", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            return result;
        }

        /// <summary>
        /// 构建order by语句
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        protected override string BuilderTempTableOrderBySQL(IRptParams filter)
        {
            //排序
            string datasort = Convert.ToString(filter.FilterParameter.SortString);
            string OrderBy = "";
            if (datasort != "")
            {
                OrderBy = datasort;
            }
            else
            {
                switch (FSummaryBasis)
                {
                    case "1":
                        //SummaryBasisField = "FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName";
                        OrderBy = " FMaterialNumber ";
                        break;
                    case "2":
                        //SummaryBasisField = "FCustNumber,FCustName";
                        OrderBy = " FCustNumber ";
                        break;
                    case "3":
                        //SummaryBasisField = "FCustGroupNumber,FCustGroupName";
                        OrderBy = " FCustGroupNumber ";
                        break;
                    case "4":
                        //SummaryBasisField = "FMaterialGroupNumber,FMaterialGroupName";
                        OrderBy = " FMaterialGroupNumber ";
                        break;
                    case "5":
                        //SummaryBasisField = "FKHSYBNumber,FKHSYBName";
                        OrderBy = " FKHSYBNumber ";
                        break;
                    case "6":
                        //SummaryBasisField = "FBMSYBNumber,FBMSYBName";
                        OrderBy = " FBMSYBNumber ";
                        break;
                    default:
                        //SummaryBasisField = "FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName";
                        OrderBy = " FMaterialNumber ";
                        break;
                }
            }
            return OrderBy;
        }

        /// <summary>
        /// 构建单据select 和字段
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        protected override string BuilderSelectFieldSQL(IRptParams filter)
        {
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            SetFilterFields(customFilter);


            string sqlstr = $@"/*dialect*/select {SummaryBasisField}, 
	{HeaderFieldTax}
	FSalesNetProfit,  FSalesNetProfitLv, FInvQty, FInvAmount ";
            return sqlstr;
        }


        /// <summary>
        /// 构建from where语句
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        protected override string BuilderFromWhereSQL(IRptParams filter)
        {
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            StringBuilder strwhere = new StringBuilder();
            SetFilterFields(customFilter);

            string _FSummaryBasis = (FSummaryBasis == "5" || FSummaryBasis == "6" ? "1" : "2");

            string invQtySql = "0 FInvQty,0 FInvPrice, 0 FInvAmount";
            string invQtySqlGroup = "";
            if (FSummaryBasis == "1" || FSummaryBasis == "4" || FSummaryBasis == "6")
            {
                invQtySql = "isnull(FInvQty,0) FInvQty,cast(sum(FPurAmount) / case when sum(FSaleQty) = 0 then 999999999999999 else sum(FSaleQty) end as decimal(18,6)) FInvPrice, cast(isnull(FInvQty,0) * sum(FPurAmount) / case when sum(FSaleQty) = 0 then 999999999999999 else sum(FSaleQty) end as decimal(18,2)) FInvAmount";
                invQtySqlGroup = ",isnull(inv.FInvQty,0)";
            }
            string invNumber = "";

            if (FSummaryBasis == "1")
            {
                invNumber = " bm.FNUMBER ";
            }
            else if (FSummaryBasis == "4")
            {
                invNumber = " bmg.FNUMBER ";
            }
            else
            {
                invNumber = " bmsyb.FNUMBER ";
            }
            strwhere.AppendLine($@" from (
select {SummaryBasisField},
	--FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName,
	--FCustNumber,FCustName,
	--FCustGroupNumber,FCustGroupName,
	--FKHSYBNumber,FKHSYBName, 
	--FBMSYBNumber,FBMSYBName,
    --FMaterialGroupNumber,FMaterialGroupName,
	sum(FSaleQty) FSaleQty,cast(sum(FSaleAmount) / case when sum(FSaleQty) = 0 then 999999999999999 else sum(FSaleQty) end as decimal(18,6)) FSalePrice,
	 cast(sum(FSaleAmount) as decimal(18,2)) FSaleAmount, cast(sum(FSaleTaxAmount) as decimal(18,2)) FSaleTaxAmount, cast(sum(FSaleAllAmount) as decimal(18,2)) FSaleAllAmount,
	cast(sum(FPurAmount) / case when sum(FSaleQty) = 0 then 999999999999999 else sum(FSaleQty) end as decimal(18,6)) FPurPrice,
    cast(sum(FPurAmount) as decimal(18,2)) FPurAmount,cast(sum(FPurTaxAmount) as decimal(18,2)) FPurTaxAmount,cast(sum(FPurAllAmount) as decimal(18,2)) FPurAllAmount,
	cast(sum(FSaleAmount)-sum(FPurAmount) as decimal(18,2)) FNoTaxProfit,cast(sum(FSaleTaxAmount) - sum(FPurTaxAmount) as decimal(18,2))  FTaxDiffAmount,
	{grossProfitMarginCalcField} FGrossProfit,
	isnull(gssybqjfy.FAMOUNT,0) FPeriodEexpense, cast(sum(FSaleAmount)-sum(FPurAmount) - isnull(gssybqjfy.FAMOUNT,0) as decimal(18,2))  FSalesNetProfit, 
	{jGrossProfitMarginCalcField} FSalesNetProfitLv,
    {invQtySql}
from (
select saleTable.FMaterialNumber,saleTable.FMaterialName,saleTable.FMaterialSpecification,saleTable.FUnitName,
		FCustNumber,FCustName,
		FCustGroupNumber,FCustGroupName,
		FKHSYBNumber,FKHSYBName, 
		FBMSYBNumber,FBMSYBName,
        FMaterialGroupNumber,FMaterialGroupName,
	saleTable.FSaleQty,saleTable.FSalePrice,saleTable.FSaleAmount,saleTable.FSaleTaxAmount,saleTable.FSaleAllAmount,
	isnull(purTable.FPurPrice, saleTableAvgPrice.FSalePrice) FPurPrice,isnull(purTable.FPurPrice, saleTableAvgPrice.FSalePrice) * saleTable.FSaleQty FPurAmount,
	isnull(purTable.FPurTaxPrice, saleTableAvgPrice.FSaleTaxPrice) * saleTable.FSaleQty - isnull(purTable.FPurPrice, saleTableAvgPrice.FSalePrice) * saleTable.FSaleQty FPurTaxAmount,
	isnull(purTable.FPurTaxPrice, saleTableAvgPrice.FSaleTaxPrice) * saleTable.FSaleQty FPurAllAmount

from 
(
	select FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName,
		FCustNumber,FCustName,
		FCustGroupNumber,FCustGroupName,
		FKHSYBNumber,FKHSYBName, 
		FBMSYBNumber,FBMSYBName,
        FMaterialGroupNumber,FMaterialGroupName,
        sum(FSaleQty) FSaleQty,	cast(sum(FAmount) / case when sum(FSaleQty) = 0 then 999999999999999 else sum(FSaleQty) end  as decimal(18,6)) FSalePrice,
		sum(FAmount) FSaleAmount, sum(FTaxAmount) FSaleTaxAmount,sum(FSaleAllAmount) FSaleAllAmount
	from (");

            if (FAmountSource == "1")
            {

                strwhere.AppendLine($@"select bm.FNUMBER FMaterialNumber, bml.FNAME FMaterialName, bml.FSPECIFICATION FMaterialSpecification,bul.FNAME FUnitName,
			bcust.FNUMBER FCustNumber,bcustl.FNAME FCustName,bcg.FNUMBER FCustGroupNumber,bcgl.FNAME FCustGroupName, khsyb.FNUMBER FKHSYBNumber,  khsybl.FNAME FKHSYBName, 
			bmg.FNUMBER FMaterialGroupNumber,bmgl.fname FMaterialGroupName, bmsyb.FNUMBER FBMSYBNumber,  bmsybl.FNAME FBMSYBName, 
            sose.FREALQTY FSaleQty, sosef.FAMOUNT_LC FAmount, sosef.FTAXAMOUNT_LC FTaxAmount,sosef.FALLAMOUNT_LC FSaleAllAmount
		from T_SAL_OUTSTOCK sos with(nolock)
		left join T_SAL_OUTSTOCKENTRY sose with(nolock) on sose.FID= sos.FID
		left join T_SAL_OUTSTOCKENTRY_F sosef with(nolock) on sosef.FENTRYID= sose.FENTRYID
		left join T_BD_MATERIAL bm with(nolock) on bm.FMATERIALID = sose.FMATERIALID
		left join T_BD_MATERIAL_L bml with(nolock) on bml.FMATERIALID = bm.FMATERIALID and bml.FLOCALEID = 2052
		left join T_BD_UNIT_L bul with(nolock) on bul.FUNITID = sose.FUNITID  and bul.FLOCALEID = 2052
		left join T_BD_CUSTOMER bcust with(nolock) on bcust.FCUSTID = sos.FCUSTOMERID
		left join T_BD_CUSTOMER_L bcustl with(nolock) on bcustl.FCUSTID = sos.FCUSTOMERID and bcustl.FLOCALEID = 2052
		left join T_BD_CUSTOMERGROUP bcg with(nolock) on bcg.FID = bcust.FPRIMARYGROUP
		left join T_BD_CUSTOMERGROUP_L bcgl with(nolock) on bcgl.FID = bcust.FPRIMARYGROUP and bcgl.FLOCALEID = 2052
		left join T_BD_DEPARTMENT khsyb on khsyb.FDEPTID = bcust.F_UIXC_GSSYB
		left join T_BD_DEPARTMENT_L khsybl on khsybl.FDEPTID = bcust.F_UIXC_GSSYB and khsybl.FLOCALEID = 2052
		left join T_BD_MATERIALGROUP bmg on bmg.FID = bm.FMATERIALGROUP 
		left join T_BD_MATERIALGROUP_L bmgl on bmgl.FID = bm.FMATERIALGROUP and bmgl.FLOCALEID = 2052
		left join T_BD_DEPARTMENT bmsyb on bmsyb.FDEPTID = bm.F_UIXC_GSSYB
		left join T_BD_DEPARTMENT_L bmsybl on bmsybl.FDEPTID = bm.F_UIXC_GSSYB and bmsybl.FLOCALEID = 2052
		where sos.FDOCUMENTSTATUS = 'C' and sos.FDATE between '{startDate}' and '{endDate}'  AND soseF.FROWTYPE = 'Standard'
       and ( bcust.FNUMBER >= '{startCust}' or isnull('{startCust}','') = '') and (bcust.FNUMBER <= '{endCust}' or isnull('{endCust}','') = '')
       and ( bm.FNUMBER >= '{startMaterial}' or isnull('{startMaterial}','') = '') and (bm.FNUMBER <= '{endMaterial}' or isnull('{endMaterial}','') = '')
		union all
		select bm.FNUMBER FMaterialNumber, bml.FNAME FMaterialName, bml.FSPECIFICATION FMaterialSpecification,bul.FNAME FUnitName,
			bcust.FNUMBER FCustNumber,bcustl.FNAME FCustName,bcg.FNUMBER FCustGroupNumber,bcgl.FNAME FCustGroupName, khsyb.FNUMBER FKHSYBNumber,  khsybl.FNAME FKHSYBName, 
			bmg.FNUMBER FMaterialGroupNumber,bmgl.fname FMaterialGroupName, bmsyb.FNUMBER FBMSYBNumber,  bmsybl.FNAME FBMSYBName, 
            -sose.FREALQTY FSaleQty, -sosef.FAMOUNT_LC FAmount, -sosef.FTAXAMOUNT_LC FTaxAmount,
			-sosef.FALLAMOUNT_LC FSaleAllAmount
		from T_SAL_RETURNSTOCK sos with(nolock)
		join T_SAL_RETURNSTOCKENTRY sose with(nolock) on sose.FID= sos.FID
		join T_SAL_RETURNSTOCKENTRY_F sosef with(nolock) on sosef.FENTRYID= sose.FENTRYID
		join T_BD_MATERIAL bm with(nolock) on bm.FMATERIALID = sose.FMATERIALID
		join T_BD_MATERIAL_L bml with(nolock) on bml.FMATERIALID = bm.FMATERIALID and bml.FLOCALEID = 2052
		join T_BD_UNIT_L bul with(nolock) on bul.FUNITID = sose.FUNITID  and bul.FLOCALEID = 2052
		join T_BD_CUSTOMER bcust with(nolock) on bcust.FCUSTID = sos.FRETCUSTID
		join T_BD_CUSTOMER_L bcustl with(nolock) on bcustl.FCUSTID = sos.FRETCUSTID and bcustl.FLOCALEID = 2052
		left join T_BD_CUSTOMERGROUP bcg with(nolock) on bcg.FID = bcust.FPRIMARYGROUP
		left join T_BD_CUSTOMERGROUP_L bcgl with(nolock) on bcgl.FID = bcust.FPRIMARYGROUP and bcgl.FLOCALEID = 2052
		left join T_BD_DEPARTMENT khsyb on khsyb.FDEPTID = bcust.F_UIXC_GSSYB
		left join T_BD_DEPARTMENT_L khsybl on khsybl.FDEPTID = bcust.F_UIXC_GSSYB and khsybl.FLOCALEID = 2052
		left join T_BD_MATERIALGROUP bmg on bmg.FID = bm.FMATERIALGROUP 
		left join T_BD_MATERIALGROUP_L bmgl on bmgl.FID = bm.FMATERIALGROUP and bmgl.FLOCALEID = 2052
		left join T_BD_DEPARTMENT bmsyb on bmsyb.FDEPTID = bm.F_UIXC_GSSYB
		left join T_BD_DEPARTMENT_L bmsybl on bmsybl.FDEPTID = bm.F_UIXC_GSSYB and bmsybl.FLOCALEID = 2052
		where sos.FDOCUMENTSTATUS = 'C' and sos.FDATE between '{startDate}' and '{endDate}'  AND soseF.FROWTYPE = 'Standard'
       and ( bcust.FNUMBER >= '{startCust}' or isnull('{startCust}','') = '') and (bcust.FNUMBER <= '{endCust}' or isnull('{endCust}','') = '')
       and ( bm.FNUMBER >= '{startMaterial}' or isnull('{startMaterial}','') = '') and (bm.FNUMBER <= '{endMaterial}' or isnull('{endMaterial}','') = '') 
	");
            }
            else
            {
                strwhere.AppendLine($@"select bm.FNUMBER FMaterialNumber, bml.FNAME FMaterialName, bml.FSPECIFICATION FMaterialSpecification,bul.FNAME FUnitName,
			bcust.FNUMBER FCustNumber,bcustl.FNAME FCustName,bcg.FNUMBER FCustGroupNumber,bcgl.FNAME FCustGroupName, khsyb.FNUMBER FKHSYBNumber,  khsybl.FNAME FKHSYBName, 
			bmg.FNUMBER FMaterialGroupNumber,bmgl.fname FMaterialGroupName, bmsyb.FNUMBER FBMSYBNumber,  bmsybl.FNAME FBMSYBName, 
            sose.FPriceQty FSaleQty, sose.FNOTAXAMOUNT FAmount, sose.FTAXAMOUNT FTaxAmount,sose.FALLAMOUNT FSaleAllAmount
		from t_AR_receivable sos with(nolock)
		join t_AR_receivableentry sose with(nolock) on sose.FID= sos.FID
		join T_BD_MATERIAL bm with(nolock) on bm.FMATERIALID = sose.FMATERIALID
		join T_BD_MATERIAL_L bml with(nolock) on bml.FMATERIALID = bm.FMATERIALID and bml.FLOCALEID = 2052
		join T_BD_UNIT_L bul with(nolock) on bul.FUNITID = sose.FPRICEUNITID  and bul.FLOCALEID = 2052
		left join T_BD_CUSTOMER bcust with(nolock) on bcust.FCUSTID = sos.FCUSTOMERID
		left join T_BD_CUSTOMER_L bcustl with(nolock) on bcustl.FCUSTID = sos.FCUSTOMERID and bcustl.FLOCALEID = 2052
		left join T_BD_CUSTOMERGROUP bcg with(nolock) on bcg.FID = bcust.FPRIMARYGROUP
		left join T_BD_CUSTOMERGROUP_L bcgl with(nolock) on bcgl.FID = bcust.FPRIMARYGROUP and bcgl.FLOCALEID = 2052
		left join T_BD_DEPARTMENT khsyb WITH(NOLOCK)  on khsyb.FDEPTID = bcust.F_UIXC_GSSYB
		left join T_BD_DEPARTMENT_L khsybl WITH(NOLOCK)  on khsybl.FDEPTID = bcust.F_UIXC_GSSYB and khsybl.FLOCALEID = 2052
		left join T_BD_MATERIALGROUP bmg  WITH(NOLOCK) on bmg.FID = bm.FMATERIALGROUP 
		left join T_BD_MATERIALGROUP_L bmgl  WITH(NOLOCK) on bmgl.FID = bm.FMATERIALGROUP and bmgl.FLOCALEID = 2052
		left join T_BD_DEPARTMENT bmsyb WITH(NOLOCK)  on bmsyb.FDEPTID = bm.F_UIXC_GSSYB
		left join T_BD_DEPARTMENT_L bmsybl  WITH(NOLOCK) on bmsybl.FDEPTID = bm.F_UIXC_GSSYB and bmsybl.FLOCALEID = 2052

		where sos.FDOCUMENTSTATUS = 'C' and sos.FDATE between '{startDate}' and '{endDate}' 
       and ( bcust.FNUMBER >= '{startCust}' or isnull('{startCust}','') = '') and (bcust.FNUMBER <= '{endCust}' or isnull('{endCust}','') = '')
       and ( bm.FNUMBER >= '{startMaterial}' or isnull('{startMaterial}','') = '') and (bm.FNUMBER <= '{endMaterial}' or isnull('{endMaterial}','') = '') 
	");
            }
            strwhere.AppendLine($@"
) t
	group by FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName,FCustNumber,FCustName,FCustGroupNumber,FCustGroupName,FKHSYBNumber,FKHSYBName,FBMSYBNumber,FBMSYBName,FMaterialGroupNumber,FMaterialGroupName
) saleTable
left join (

    select FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName,sum(FPurQty) FPurQty,
		cast(sum(FPurAmount) / case when  sum(FPurQty) = 0 then 9999999999 else sum(FPurQty) end as decimal(18,6)) FPurPrice,
		cast(sum(FPurAllAmount) / case when  sum(FPurQty) = 0 then 9999999999 else sum(FPurQty) end as decimal(18,6)) FPurTaxPrice
	from (");

            if (FAmountSource == "1")
            {
                strwhere.AppendLine($@"
		select bm.FNUMBER FMaterialNumber, bml.FNAME FMaterialName, bml.FSPECIFICATION FMaterialSpecification,bul.FNAME FUnitName,
			sise.FREALQTY FPurQty, sisef.FAMOUNT_LC FPurAmount, sisef.FTAXAMOUNT_LC FPurTaxAmount,sisef.FALLAMOUNT_LC FPurAllAmount
		from t_STK_InStock sis with(nolock)
		join T_STK_INSTOCKENTRY sise with(nolock) on sise.FID = sis.FID
		join T_STK_INSTOCKENTRY_F sisef with(nolock) on sisef.FENTRYID = sise.FENTRYID
		join T_BD_MATERIAL bm with(nolock) on bm.FMATERIALID = sise.FMATERIALID
		join T_BD_MATERIAL_L bml with(nolock) on bml.FMATERIALID = bm.FMATERIALID and bml.FLOCALEID = 2052
		join T_BD_UNIT_L bul with(nolock) on bul.FUNITID = sise.FUNITID and bul.FLOCALEID = 2052
		where sis.FDOCUMENTSTATUS = 'C'  and sis.FDATE between '{startDate}' and '{endDate}'
        and ( bm.FNUMBER >= '{startMaterial}' or isnull('{startMaterial}','') = '') and (bm.FNUMBER <= '{endMaterial}' or isnull('{endMaterial}','') = '') 
		union all
		select bm.FNUMBER FMaterialNumber, bml.FNAME FMaterialName, bml.FSPECIFICATION FMaterialSpecification,bul.FNAME FUnitName,
			-sise.FRMREALQTY FPurQry, -sisef.FAMOUNT_LC FPurAmount, -sisef.FTAXAMOUNT_LC FPurTaxAmount,-sisef.FALLAMOUNT_LC FPurAllAmount
		from t_PUR_MRB sis with(nolock)
		join T_PUR_MRBENTRY sise with(nolock) on sise.FID = sis.FID
		join T_PUR_MRBENTRY_F sisef with(nolock) on sisef.FENTRYID = sise.FENTRYID
		join V_BD_BUYER_L buyerl with(nolock) on buyerl.fid = sis.FPURCHASERID and buyerl.FLOCALEID = 2052 
		join T_BD_MATERIAL bm with(nolock) on bm.FMATERIALID = sise.FMATERIALID
		join T_BD_MATERIAL_L bml with(nolock) on bml.FMATERIALID = bm.FMATERIALID and bml.FLOCALEID = 2052
		join T_BD_UNIT_L bul with(nolock) on bul.FUNITID = sise.FUNITID and bul.FLOCALEID = 2052
		where sis.FDOCUMENTSTATUS = 'C'  and sis.FDATE between '{startDate}' and '{endDate}'
        and( bm.FNUMBER >= '{startMaterial}' or isnull('{startMaterial}','') = '') and (bm.FNUMBER <= '{endMaterial}' or isnull('{endMaterial}','') = '') ");
            }
            else
            {
                strwhere.AppendLine($@"		select bm.FNUMBER FMaterialNumber, bml.FNAME FMaterialName, bml.FSPECIFICATION FMaterialSpecification,bul.FNAME FUnitName,
			sise.FPRICEQTY FPurQty, sise.FNOTAXAMOUNT FPurAmount, sise.FTAXAMOUNT FPurTaxAmount,sise.FALLAMOUNT FPurAllAmount
		from T_AP_PAYABLE sis with(nolock)
		join T_AP_PAYABLEENTRY sise with(nolock) on sise.FID = sis.FID
		join T_BD_MATERIAL bm with(nolock) on bm.FMATERIALID = sise.FMATERIALID
		join T_BD_MATERIAL_L bml with(nolock) on bml.FMATERIALID = bm.FMATERIALID and bml.FLOCALEID = 2052
		join T_BD_UNIT_L bul with(nolock) on bul.FUNITID = sise.FPRICEUNITID and bul.FLOCALEID = 2052
		where sis.FDOCUMENTSTATUS = 'C'  and sis.FDATE between '{startDate}' and '{endDate}'
        and( bm.FNUMBER >= '{startMaterial}' or isnull('{startMaterial}','') = '') and (bm.FNUMBER <= '{endMaterial}' or isnull('{endMaterial}','') = '') ");
            }
            strwhere.AppendLine($@"
) t
	group by FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName
) purTable on purTable.FMaterialNumber = saleTable.FMaterialNumber
left join 
(
	select bm.FNUMBER FMaterialNumber, 
			t.FPRICE FSalePrice, t.FTAXPRICE FSaleTaxPrice,
			ROW_NUMBER() over(partition by bm.FNUMBER order by t.fentryid desc ) id
	from T_PUR_PRICELISTENTRY T WITH(NOLOCK)
    join T_PUR_PRICELIST t1 WITH(NOLOCK) on t1.fid = t.fid
	JOIN T_BD_MATERIAL bm  WITH(NOLOCK) ON bm.FMATERIALID = T.FMATERIALID
	JOIN T_BD_MATERIAL_L bml WITH(NOLOCK) ON bml.FMATERIALID = T.FMATERIALID AND bml.FLOCALEID = 2052
	join T_BD_UNIT_L bul with(nolock) on bul.FUNITID = T.FUNITID and bul.FLOCALEID = 2052
	where 1=1 --and t.FENTRYID in (select max(FENTRYID) from  T_PUR_PRICELISTENTRY WITH(NOLOCK) group by FMATERIALID) 
	and t1.FDOCUMENTSTATUS = 'C'  and t1.FNUMBER ='CGJM000021'
    and ( bm.FNUMBER >= '{startMaterial}' or isnull('{startMaterial}','') = '') and (bm.FNUMBER <= '{endMaterial}' or isnull('{endMaterial}','') = '') 
) saleTableAvgPrice on saleTableAvgPrice.FMaterialNumber = saleTable.FMaterialNumber and saleTableAvgPrice.id = 1
) maint 
left join (select t3.FNUMBER FDeptNumber, sum(t1.FAMOUNT) FAMOUNT
from T_GL_VOUCHER t with(nolock)
join T_GL_VOUCHERENTRY t1 with(nolock) on t1.FVOUCHERID = t.FVOUCHERID 
join T_BD_FLEXITEMDETAILV t2 with(nolock) on t2.FID = t1.FDETAILID
join T_BD_DEPARTMENT t3 with(nolock) on t3.FDEPTID = t2.FFLEX5
where t1.FACCOUNTID in (select FACCTID from T_BD_ACCOUNT where FGROUPID = 3010)
and t.FDATE between '{startDate}' and '{endDate}' and 1 = {_FSummaryBasis}
group by t3.FNUMBER ) gssybqjfy on gssybqjfy.FDeptNumber = {GSSYBField}
left join (
	select {invNumber} FInvNumber,
        --bm.FNUMBER FInvNumber,
        --bmg.FNUMBER FInvNumber, 
        --bmsyb.FNUMBER FInvNumber,
        sum(FBASEQTY) FInvQty
	from T_STK_INVENTORY t with(nolock)
	left join T_BD_MATERIAL bm with(nolock) on bm.FMATERIALID = t.FMATERIALID
	left join T_BD_MATERIALGROUP bmg  WITH(NOLOCK) on bmg.FID = bm.FMATERIALGROUP 
	left join T_BD_MATERIALGROUP_L bmgl  WITH(NOLOCK) on bmgl.FID = bm.FMATERIALGROUP and bmgl.FLOCALEID = 2052
	left join T_BD_DEPARTMENT bmsyb WITH(NOLOCK)  on bmsyb.FDEPTID = bm.F_UIXC_GSSYB
	left join T_BD_DEPARTMENT_L bmsybl  WITH(NOLOCK) on bmsybl.FDEPTID = bm.F_UIXC_GSSYB and bmsybl.FLOCALEID = 2052
	group by  {invNumber} having sum(FBASEQTY) > 0
) inv on isnull(inv.FInvNumber,'') = isnull( {invJoinField},'') 
group by isnull(gssybqjfy.FAMOUNT,0),{SummaryBasisField} {invQtySqlGroup}
--FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName
--FCustNumber,FCustName


) t1  Where 1=1 ");

            string execSql = strwhere.ToString();
            return execSql;
        }

        private void SetFilterFields(DynamicObject customFilter)
        {
            startDate = (customFilter["FStartDate"] == null)
               ? DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd")
               : Convert.ToDateTime(customFilter["FStartDate"]).ToString("yyyy-MM-dd");
            endDate = (customFilter["FEndDate"] == null)
               ? DateTime.Now.ToString("yyyy-MM-dd")
               : Convert.ToDateTime(customFilter["FEndDate"]).ToString("yyyy-MM-dd");
            multiOrgNameValues = this.GetMultiOrgnNameValues(customFilter["FOrgId"].ToString());
            startCust = this.getBaseDataNumberValue(customFilter["FStartCustNumber"] as DynamicObject);
            endCust = this.getBaseDataNumberValue(customFilter["FEndCustNumber"] as DynamicObject);
            startMaterial = this.getBaseDataNumberValue(customFilter["FStartMaterialNumber"] as DynamicObject);
            endMaterial = this.getBaseDataNumberValue(customFilter["FEndMaterialNumber"] as DynamicObject);

            FAmountSource = customFilter["FAmountSource"].ToString();
            FSummaryBasis = customFilter["FSummaryBasis"].ToString();
            FGrossProfitMarginCalc = customFilter["FGrossProfitMarginCalc"].ToString();

            SummaryBasisField = "FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName";
            switch (FSummaryBasis)
            {
                case "1":
                    SummaryBasisField = "FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName,FBMSYBNumber,FBMSYBName";
                    GSSYBField = "maint.FKHSYBNumber";
                    invJoinField = "maint.FMaterialNumber";
                    break;
                case "2":
                    SummaryBasisField = "FCustNumber,FCustName,FKHSYBNumber,FKHSYBName";
                    GSSYBField = "maint.FKHSYBNumber";
                    invJoinField = "maint.FMaterialNumber";
                    break;
                case "3":
                    SummaryBasisField = "FCustGroupNumber,FCustGroupName";
                    GSSYBField = "maint.FKHSYBNumber";
                    invJoinField = "maint.FMaterialNumber";
                    break;
                case "4":
                    SummaryBasisField = "FMaterialGroupNumber,FMaterialGroupName";
                    GSSYBField = "maint.FKHSYBNumber";
                    invJoinField = "maint.FMaterialGroupNumber";
                    break;
                case "5":
                    SummaryBasisField = "FKHSYBNumber,FKHSYBName";
                    GSSYBField = "maint.FKHSYBNumber";
                    invJoinField = "maint.FMaterialNumber";
                    break;
                case "6":
                    SummaryBasisField = "FBMSYBNumber,FBMSYBName";
                    GSSYBField = "maint.FBMSYBNumber";
                    invJoinField = "maint.FBMSYBNumber";
                    break;
                default:
                    SummaryBasisField = "FMaterialNumber,FMaterialName,FMaterialSpecification,FUnitName";
                    break;
            }

            grossProfitMarginCalcField = "";
            switch (FGrossProfitMarginCalc)
            {
                case "1":
                    grossProfitMarginCalcField = " (sum(FSaleAllAmount) - sum(FPurAllAmount)) * 100.0 / case when sum(FSaleAllAmount) = 0 then 999999999999 else sum(FSaleAllAmount) end ";
                    jGrossProfitMarginCalcField = " (sum(FSaleAllAmount) - sum(FPurAllAmount) - isnull(gssybqjfy.FAMOUNT,0)) * 100.0 / case when sum(FSaleAllAmount) = 0 then 999999999999 else sum(FSaleAllAmount) end ";
                    HeaderFieldTax = @"
	FSaleQty,FSalePrice,FSaleAmount, FSaleTaxAmount, FSaleAllAmount,
	FPurPrice,FPurAmount,FPurTaxAmount,FPurAllAmount,
	FNoTaxProfit,FTaxDiffAmount,FGrossProfit,FPeriodEexpense,
";
                    break;
                case "2":
                    grossProfitMarginCalcField = " (sum(FSaleAmount) - sum(FPurAmount)) * 100.0 / case when sum(FSaleAmount) = 0 then 999999999999 else sum(FSaleAmount) end ";
                    jGrossProfitMarginCalcField = " (sum(FSaleAmount) - sum(FPurAmount)- isnull(gssybqjfy.FAMOUNT,0)) * 100.0 / case when sum(FSaleAmount) = 0 then 999999999999 else sum(FSaleAmount) end ";
                    HeaderFieldTax = @"
	FSaleQty,FSalePrice,FSaleAmount,0 FSaleTaxAmount,0 FSaleAllAmount,
	FPurPrice,FPurAmount,0 FPurTaxAmount,0 FPurAllAmount,
	FNoTaxProfit,0 FTaxDiffAmount,FGrossProfit,FPeriodEexpense,
";
                    break;
                case "3":
                    grossProfitMarginCalcField = " (sum(FSaleAllAmount) - sum(FPurAllAmount)) * 100.0 / case when sum(FPurAllAmount) = 0 then 999999999999 else sum(FPurAllAmount) end ";
                    jGrossProfitMarginCalcField = " (sum(FSaleAllAmount) - sum(FPurAllAmount)- isnull(gssybqjfy.FAMOUNT,0)) * 100.0 / case when sum(FPurAllAmount) = 0 then 999999999999 else sum(FPurAllAmount) end ";
                    HeaderFieldTax = @"
	FSaleQty,FSalePrice,FSaleAmount, FSaleTaxAmount, FSaleAllAmount,
	FPurPrice,FPurAmount,FPurTaxAmount,FPurAllAmount,
	FNoTaxProfit,FTaxDiffAmount,FGrossProfit,FPeriodEexpense,
";
                    break;
                case "4":
                    grossProfitMarginCalcField = " (sum(FSaleAmount) - sum(FPurAmount)) * 100.0 / case when sum(FPurAmount) = 0 then 999999999999 else sum(FPurAmount) end ";
                    jGrossProfitMarginCalcField = " (sum(FSaleAmount) - sum(FPurAmount)- isnull(gssybqjfy.FAMOUNT,0)) * 100.0 / case when sum(FPurAmount) = 0 then 999999999999 else sum(FPurAmount) end ";
                    HeaderFieldTax = @"
	FSaleQty,FSalePrice,FSaleAmount,0 FSaleTaxAmount,0 FSaleAllAmount,
	FPurPrice,FPurAmount,0 FPurTaxAmount,0 FPurAllAmount,
	FNoTaxProfit,0 FTaxDiffAmount,FGrossProfit,FPeriodEexpense,
";
                    break;
                default:
                    grossProfitMarginCalcField = " (sum(FSaleAllAmount) - sum(FPurAllAmount)) * 100.0 / case when sum(FSaleAllAmount) = 0 then 999999999999 else sum(FSaleAllAmount) end ";
                    jGrossProfitMarginCalcField = " (sum(FSaleAllAmount) - sum(FPurAllAmount)- isnull(gssybqjfy.FAMOUNT,0)) * 100.0 / case when sum(FSaleAllAmount) = 0 then 999999999999 else sum(FSaleAllAmount) end ";
                    HeaderFieldTax = @"
	FSaleQty,FSalePrice,FSaleAmount, FSaleTaxAmount, FSaleAllAmount,
	FPurPrice,FPurAmount,FPurTaxAmount,FPurAllAmount,
	FNoTaxProfit,FTaxDiffAmount,FGrossProfit,FPeriodEexpense,
";
                    break;
            }

        }


        #endregion

        #region 公共方法

        /// <summary>
        /// 物料分组编码
        /// </summary>
        /// <param name="FSMaterialGroup"></param>
        /// <param name="FEMaterialGroup"></param>
        /// <returns></returns>
        public string getFilterMaterialGroup(string FSMaterialGroup, string FEMaterialGroup)
        {
            HashSet<string> setGroup = new HashSet<string>();
            if (!string.IsNullOrWhiteSpace(FSMaterialGroup) || !string.IsNullOrWhiteSpace(FEMaterialGroup))
            {
                string sql = "select FID from T_BD_MATERIALGROUP where  1=1  ";
                if (!string.IsNullOrWhiteSpace(FSMaterialGroup))
                {
                    sql += $" and FNUMBER >='{FSMaterialGroup}' ";
                }

                if (!string.IsNullOrWhiteSpace(FEMaterialGroup))
                {
                    sql += $" and FNUMBER <='{FEMaterialGroup}' ";
                }
                DynamicObjectCollection mGroup = DBUtils.ExecuteDynamicObject(Context, sql);
                if (mGroup != null)
                {
                    foreach (var item in mGroup)
                    {
                        string sql1 = $"select FID from T_BD_MATERIALGROUP where fid = {item["FID"].ToString()} or FFULLPARENTID like '%{item["FID"].ToString()}%'";
                        DynamicObjectCollection CmGroup = DBUtils.ExecuteDynamicObject(Context, sql);
                        foreach (var item1 in CmGroup)
                        {
                            setGroup.Add(item1["FID"].ToString());
                        }
                    }
                }
            }
            string strMapGroup = "'0'";
            foreach (var item in setGroup)
            {
                strMapGroup += $",'{item}'";
            }
            if (setGroup.Count > 0)
            {
                return $" and FMATERIALGROUPID in ({strMapGroup})";
            }
            return "";
        }

        /// <summary>
        /// 组织名称
        /// </summary>
        /// <param name="orgIdStrings"></param>
        /// <returns></returns>
        private string GetMultiOrgnNameValues(string orgIdStrings)
        {
            List<string> list = new List<string>();
            string result = string.Empty;
            if (orgIdStrings.Trim().Length > 0)
            {
                IQueryService service = Kingdee.BOS.Contracts.ServiceFactory.GetService<IQueryService>(base.Context);
                QueryBuilderParemeter para = new QueryBuilderParemeter
                {
                    FormId = "ORG_Organizations",
                    SelectItems = SelectorItemInfo.CreateItems("FNAME"),
                    FilterClauseWihtKey = string.Format(" FORGID IN ({0}) AND FLOCALEID={1}", orgIdStrings, base.Context.UserLocale.LCID)
                };
                DynamicObjectCollection dynamicObjectCollection = service.GetDynamicObjectCollection(base.Context, para, null);
                foreach (DynamicObject current in dynamicObjectCollection)
                {
                    list.Add(current["FNAME"].ToString());
                }
                if (list.Count > 0)
                {
                    result = string.Join(",", list.ToArray());
                }
            }
            return result;
        }

        /// <summary>
        /// 基础资料名称
        /// </summary>
        /// <param name="dyobj"></param>
        /// <returns></returns>
        private string GetBaseDataNameValue(DynamicObjectCollection dyobj)
        {
            string name = "";
            foreach (DynamicObject dynbj in dyobj)
            {
                if (dynbj != null || !dynbj.DynamicObjectType.Properties.Contains("Name"))
                {
                    DynamicObject dynbj2 = (DynamicObject)dynbj[2];
                    name = name + ",'" + dynbj2["Name"].ToString() + "'";
                }
            }
            if (name.Length > 0)
            {
                name = name.Substring(1, name.Length - 1);
            }
            return name;
        }

        /// <summary>
        /// 获取 基础资料 编码
        /// </summary>
        /// <param name="dyobj"></param>
        /// <returns></returns>
        public string getBaseDataNumberValue(DynamicObject dyobj)
        {
            if (dyobj == null)
            {
                return "";
            }
            string number = "";
            if (dyobj.DynamicObjectType.Properties.Contains("Number"))
            {
                number = dyobj["Number"].ToString();
            }
            return number;
        }

        #endregion
    }
}
